//
//  ZLVisitorView.swift
//  微博
//
//  Created by 朱万龙 on 2016/12/14.
//  Copyright © 2016年 zwl. All rights reserved.
//

import UIKit

//访客视图
class ZLVisitorView: UIView {
 
    /// MARK: -   btn
    //4、注册按钮
     lazy var registerButton : UIButton = {
        
        let button = UIButton()
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        button.setTitleColor(UIColor.black, for: .highlighted)
        button.setBackgroundImage(
            UIImage(named:"common_button_white_disable"), for: .normal)
        return button
    }()
    //5、登陆按钮
     lazy var loginButton : UIButton = {
        let button = UIButton()
        button.setTitle("登陆", for: .normal)
        button.setBackgroundImage(
            UIImage(named:"common_button_white_disable"), for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        return button
    }()
    
    
    /// MARK: -设置访客视图信息
    /// 使用字典设置
    /// zl_message   zl_imageName
    var visitorInfo : [String:String]?{
        didSet{
            //全空返回
            guard let message = visitorInfo?["zl_mesage"],
            let imageName = visitorInfo?["zl_imageName"]
            else {
                return
            }
            tipLable.text = message
            //imageName等于空   首页
            if imageName == "" {
                satrtAnimation()
                return
            }
            iconImageView.image = UIImage(named: imageName)
            //隐藏小房子
            houseImageView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    fileprivate func satrtAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        //旋转一圈所用时间
        anim.duration = 15
        //动画完成不删除，如果IconVIew被销毁，动画随之消失
        anim.isRemovedOnCompletion = false
        iconImageView.layer.add(anim, forKey: nil)
    }

   // MARK: - 私有控件
    //1、背景IconImage
    fileprivate lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //2、小房子
    fileprivate lazy var houseImageView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //3、提示标签
    fileprivate lazy var tipLable : UILabel = UILabel.zl_label(withText: "关注一下人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray)
   
    //6、遮罩bg
    fileprivate lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
}

//设置UI
extension ZLVisitorView{

    func setUpUI() {
        backgroundColor = UIColor.zl_color(withHex: 0xEDEDED)
        
    //1、添加控件
        addSubview(iconImageView)
        addSubview(maskIconView)
        addSubview(houseImageView)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(tipLable)
        tipLable.textAlignment = .center
        
    //2、纯代码适配，需要取消aoturesizing,aotulayout,不能共存
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
            
        }
        let margin: CGFloat = 20.0
    //3、纯代码适配（苹果原生）
        //1>bgImage（圈圈）

        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        //2>houseImage(小房子)
        addConstraint(NSLayoutConstraint(item: houseImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        //3>tipLable(提示文字)
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        //4>registerButton(注册按钮)
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        //5>loginButton(登录按钮)
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        //6、maskIconView（遮罩）VFL布局
        let viewDict = ["maskIconView":maskIconView,
                        "registerButton":registerButton] as [String : Any]
        let metrics = ["scping":-35]
        
        //views:定义VFL中的控件名称和实际名称的影射关系
        //metrics:定义VFL中的（）指定的常量映射关系
       addConstraints(NSLayoutConstraint.constraints(
             withVisualFormat: "H:|-0-[maskIconView]-0-|",
                                        options: [],
                                        metrics: nil,
                                        views: viewDict))
       addConstraints(NSLayoutConstraint.constraints(
             withVisualFormat: "V:|-0-[maskIconView]-(scping)-[registerButton]",
                                        options: [],
                                        metrics: metrics,
                                        views: viewDict))

        
    }
}
